import java.util.Arrays;

public class QuickSortTest {
    public static void quickSort(int[] arr,int low,int high){
        if (low < high) {
            System.out.println(Arrays.toString(arr));

            int pivotIndex = partition(arr, low, high);
            quickSort(arr, low, pivotIndex - 1);

            quickSort(arr, pivotIndex + 1, high);
        }
    }

    public static int partition(int []arr,int low,int high){
        int pivot = arr[low];
        int i = low + 1;
        int j = high;

        while (true) {
            while (i <= j && arr[i] <= pivot) {
                i++;
            }
            while (i <= j && arr[j] > pivot) {
                j--;
            }
            if (i <j) {
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            } else {
                break;
            }
        }
        int temp = arr[low];
        arr[low] = arr[j];
        arr[j] = temp;
        System.out.println("j="+j);
        return j;

    }

    public static void main(String[] args) {
        int[] arr = {13, 3, 18, 10, 12, 9, 15};
        quickSort(arr, 0, arr.length - 1);
        System.out.println(Arrays.toString(arr));
    }
}
